package com.zx.scp.controller;

import com.yeepay.paymobile.utils.PaymobileUtils;
import com.zx.map.service.domain.nocard.NocardCallBackDomain;
import com.zx.map.service.domain.nocard.NocardPayReturnDomain;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.map.utils.MyBeanUtils;
import com.zx.scp.service.NoCardIncomeService;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;
import java.util.TreeMap;

/**
 * 无卡支付
 *
 * @author jefferry
 */
@Controller
@RequestMapping("/nocard")
public class NocardPayController {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(NocardPayController.class);
    @Autowired
    private NoCardIncomeService noCardIncomeService;

    /**
     * 无卡收款
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/nocardCallBack")
    @ResponseBody
    public String nocardCallBack(NocardCallBackDomain nocardCallBackDomain,
                                 Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            //解密data
            log.info("返回的参数：" + ToStringBuilder.reflectionToString(nocardCallBackDomain));
            TreeMap<String, String> dataMap = PaymobileUtils.decrypt(nocardCallBackDomain.getData(), nocardCallBackDomain.getEncryptkey());
            log.info("返回的明文参数：" + dataMap);
            //sign验签
            if (!PaymobileUtils.checkSign(dataMap)) {
                log.error("无卡支付返回验签失败");
                model.addAttribute("errorMsg", "系统异常");
                return "/jsp/mobile/error";
            }
            //{amount=1, bank=招商银行, bankcode=CMBCHINA, cardtype=2, lastno=6187, merchantaccount=10012429710,
            //orderid=N201608041459780, status=1, yborderid=411608045221375101}
            String amount = new BigDecimal(dataMap.get("amount")).divide(
                    new BigDecimal("100")).toString();
            NocardPayReturnDomain nocardPayReturnDomain = new NocardPayReturnDomain();
            nocardPayReturnDomain.setOrderId(dataMap.get("orderid"));
            nocardPayReturnDomain.setAmount(amount);
            nocardPayReturnDomain.setLastNo(dataMap.get("lastno"));
            nocardPayReturnDomain.setStatus("1".equals(dataMap.get("status")) ? "S" : "F");
            nocardPayReturnDomain.setBank(dataMap.get("bank"));
            nocardPayReturnDomain.setBankCode(dataMap.get("bankcode"));
            nocardPayReturnDomain.setCardType("1".equals(dataMap.get("cardtype"))?"D":"C");
            nocardPayReturnDomain.setMerchantAccount(dataMap.get("merchantaccount"));
            nocardPayReturnDomain.setYbOrderId(dataMap.get("yborderid"));
            noCardIncomeService.notifyOrder(nocardPayReturnDomain);
        } catch (Exception e) {
            model.addAttribute("errorMsg", "系统异常");
            log.error("系统异常", e);
        }
        return "SUCCESS";
    }

    /**
     * 无卡收款  手动 强制收款
     *
     * @param model
     * @return
     */
    @RequestMapping("/orderPayNotify")
    @ResponseBody
    public Map orderPayNotify(NocardPayReturnDomain nocardPayReturnDomain, Model model) {
        try {

            log.info("订单号：" + MyBeanUtils.beanToJson(nocardPayReturnDomain));
            return noCardIncomeService.notifyOrder(nocardPayReturnDomain);
        } catch (Exception e) {
            model.addAttribute("errorMsg", "系统异常");
            log.error("系统异常", e);
            return CommonResponseUtils.failureResponse( "系统异常");
        }

    }

    /**
     * 无卡收款  手动 强制收款
     *
     * @param model
     * @return
     */
    @RequestMapping("/checkPayStatus")
    @ResponseBody
    public Map checkPayStatus(NocardPayReturnDomain nocardPayReturnDomain, Model model) {
        try {
            //解密data
            log.info("订单号：" + MyBeanUtils.beanToJson(nocardPayReturnDomain));
            return CommonResponseUtils.failureResponse("待完善");//noCardIncomeService.checkPayStatus(nocardPayReturnDomain);
        } catch (Exception e) {
            model.addAttribute("errorMsg", "系统异常");
            log.error("系统异常", e);
            return CommonResponseUtils.failureResponse( "系统异常");
        }

    }

}
